#define _CRT_SECURE_NO_WARNINGS 1
#include"SepList.h"
void SLInit(SL *p)
{
	assert(p);
	p->a = NULL;
	p->size = 0;
	p->capacity = 0;
}


void SLPushback(DataSL x, SL* p)
{
	if (p->size == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		DataSL* tmp = (DataSL*)realloc(p->a, sizeof(DataSL) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		p->a = tmp;
		p->capacity = newcapacity;
	}
	p->a[p->size] = x;
	p->size++;
}


void SLPrintf(SL* p)
{
	assert(p);
	for (int i = 0; i < p->size; i++)
	{
		printf("%d ", p->a[i]);
	}
}

void SLDestroy(SL* p)
{
	assert(p);
	if (p->a)
	{
		free(p->a);
		p->a = NULL;
		p->size = p->capacity = 0;
	}
}

void SLPopback(SL* p)
{
	assert(p);
	assert(p->size > 0);
	p->size--;
}





void SLPushfront(DataSL x, SL* p)
{
	if (p->size == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		DataSL* tmp = (DataSL*)realloc(p->a, sizeof(DataSL) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		p->a = tmp;
		p->capacity = newcapacity;
	}
	int end = p->size;
	while (end > 0)
	{
		p->a[end] = p->a[end - 1];
		end--;
	}
	p->a[0] = x;
	p->size++;
}





void SLPopfront(SL* p)
{
	assert(p);
	int begin = 1;
	while (begin < p->size)
	{
		p->a[begin - 1] = p->a[begin];
		begin++;
	}
	p->size--;
}



void SLPushpos(DataSL pos, DataSL x, SL* p)
{
	assert(pos > 0 && pos <= p->size+1);

	if (p->size == p->capacity)
	{
		int newcapacity = p->capacity == 0 ? 4 : 2 * p->capacity;
		DataSL* tmp = (DataSL*)realloc(p->a, sizeof(DataSL) * newcapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		p->a = tmp;
		p->capacity = newcapacity;
	}
	int begin = pos - 1;
	int end = p->size;
	while (end > begin)
	{
		p->a[end] = p->a[end - 1];
		end--;
	}
	p->a[end] = x;
	p->size++;

}


void SLPoppos(DataSL pos,SL* p)
{
	assert(pos >= 0 && pos < p->size);
	int begin = pos+1;
	while (begin < p->size)
	{
		p->a[begin - 1] = p->a[begin];
		begin++;
	}
	p->size--;
}


DataSL SLFind(SL* p, DataSL x,int begin)
{
	assert(p);
	/*int i = 0;
	for (i = 0; i < p->size; i++)
	{
		if (p->a[i] == x)
		{
			return i;
		}
		
	}
	return -1;*/

	int i = begin;
	for (i = 0; i < p->size; i++)
	{
		if (p->a[i] == x)
		{
			return i;
		}
	}
	return -1;

}
